home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / New System Software Extensions / ASLM SDK v1.1.2 / ASLM Examples / Example Tools / Sources / TMacSemaphoreExample.cp < prev    next >
Encoding:
Text File  |  1994-11-21  |  2.8 KB  |  92 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        TMacSemaphoreExample.cp
  3.  
  4.     Contains:    This module shows an example use of TMacSemaphore.
  5.  
  6.     Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #include "TInitSLM.h"            // the TInitSLM class and SLM include files
  11.  
  12. #include <Events.h>
  13. #include <sysequ.h>
  14.  
  15. ///————————————————————————————————————————————————————————————————————————————————————
  16. ///    PROTOTYPES
  17. ///————————————————————————————————————————————————————————————————————————————————————
  18.  
  19. static void move_cursor( Point newMouse);
  20.  
  21. /*————————————————————————————————————————————————————————————————————————————————————
  22.     main 
  23.     
  24.     since the Macintosh OS supports only one thread of execution, the only way that 
  25.     data can be changed while you are trying to access it is for the data to be changed
  26.     by an interrupt. One way to demonstrate is by changing the cursor position, since
  27.     the cursor can change at interrupt time. Not only do we have to disable interrupts
  28.     when we change the cursor position, but we also have to check to see if the cursor
  29.     is already busy. If it is then we have to re-enable interrupts so then cursor
  30.     can become "unbusy".
  31. ————————————————————————————————————————————————————————————————————————————————————*/
  32.  
  33. main() 
  34. {
  35.     TInitSLM initLibraryManager;            // initialize the shared library manager
  36.     
  37.     if( initLibraryManager.Failed() )        // If we failed, let's go home
  38.         return 1;
  39.     
  40.     TMacSemaphore        *semaphore = new TMacSemaphore;
  41.  
  42.     TStopwatch        stopwatch;
  43.     while( stopwatch.ElapsedSeconds() < 2) {    // do this for 2 seconds
  44.  
  45.         // we need to go into a loop until we can successfully grab the semaphore
  46.         // while the cursor isn't busy.
  47.         while( true ) {
  48.             semaphore->Grab();                // disable interrupts, so we can change cursor
  49.             
  50.             if( ! *(Boolean *)CrsrBusy )    // if cursor isn't busy our grab is good
  51.                 break;
  52.         
  53.             semaphore->Release();            // release and try again
  54.         }
  55.  
  56.         Point        newmouse;            
  57.         
  58.         SetPt( &newmouse, 0,0 );            // move to cursor to 0,0
  59.         move_cursor( newmouse );
  60.         
  61.         semaphore->Release();                // release
  62.     }
  63.     
  64.     delete semaphore;                        // deallocate the space used
  65.     
  66.     return 0;
  67. }
  68.  
  69. /*————————————————————————————————————————————————————————————————————————————————————
  70.     move_cursor
  71.     
  72.     move the cursor to the new position passed. Please, don't try this at home. This
  73.     is only to demonstrate the semaphores.
  74. ————————————————————————————————————————————————————————————————————————————————————*/
  75.     static void
  76. move_cursor( 
  77.     Point        newMouse )
  78. {
  79.     long         *vh;
  80.     
  81.     vh = (long *)&newMouse;
  82.     
  83.     HideCursor();                            // Hide  the cursor since we are changing it
  84.  
  85.     *((long *)RawMouse) = *vh;                // force the mouse position to top of scren
  86.     *((long *)MTemp) = *vh;                    // also the temporary position
  87.     *((short *)CrsrState) = 0;                // indicate the cursor has changed
  88.     *((char *)CrsrNew) = 1;                    // indicate a new cursor
  89.  
  90.     ShowCursor();                            // redisplay the cursor now
  91. }
  92.